﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GeoWeb.Common;
using System.Text;
using System.IO;

namespace GeoWeb.Demo.Commons
{
    /// <summary>
    /// GeoWeb.Common通用工具类示例Demo
    /// add by zhangyu
    /// 2013.01.17
    /// </summary>
    public partial class Demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //设置时间的默认值为当前时间
                Cd_SeleteDate.SelectedDate = DateTime.Now;
            }
        }

        /// <summary>
        /// 根据公历日期获取农历及各种信息
        /// add by zhangyu
        /// 2013.01.17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_Convert_Click(object sender, EventArgs e)
        {
            DateTime dtDate = Cd_SeleteDate.SelectedDate;
            ChineseCalendar c = new ChineseCalendar(dtDate);
            StringBuilder dayInfo = new StringBuilder();
            dayInfo.Append("阳历：" + c.DateString + "<br>");
            dayInfo.Append("农历：" + c.ChineseDateString + "<br>");
            dayInfo.Append("星期：" + c.WeekDayStr + "<br>");
            dayInfo.Append("时辰：" + c.ChineseHour + "<br>");
            dayInfo.Append("属相：" + c.AnimalString + "<br>");
            dayInfo.Append("节气：" + c.ChineseTwentyFourDay + "<br>");
            dayInfo.Append("前一个节气：" + c.ChineseTwentyFourPrevDay + "<br>");
            dayInfo.Append("下一个节气：" + c.ChineseTwentyFourNextDay + "<br>");
            dayInfo.Append("节日：" + c.DateHoliday + "<br>");
            dayInfo.Append("干支：" + c.GanZhiDateString + "<br>");
            dayInfo.Append("星宿：" + c.ChineseConstellation + "<br>");
            dayInfo.Append("星座：" + c.Constellation + "<br>");
            lb_chdateinfo.Text = dayInfo.ToString();
        }

        /// <summary>
        /// 汉字转换为拼音
        /// add by zhangyu
        /// 2013.01.17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_ConvertPinyin_Click(object sender, EventArgs e)
        {
            EcanConvertToCh CtoCh = new EcanConvertToCh();

            Lb_Pinyin.Text = " " + Txt_Chinese.Text.Trim() + " 转换后的拼音为：" + CtoCh.convertCh(Txt_Chinese.Text.Trim());
        }

        /// <summary>
        /// 将全角字符串转换成半角字符串
        /// add by zhangyu
        /// 2013.01.17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_ConvertQj_Click(object sender, EventArgs e)
        {
            StringHelper sh = new StringHelper();
            Txt_stringBJ.Text = sh.ToDBC(txt_stringQJ.Text);
        }

        /// <summary>
        /// 将半角字符串转换成全角字符串
        /// add by zhangyu
        /// 2013.01.17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_ConvertBj_Click(object sender, EventArgs e)
        {
            StringHelper sh = new StringHelper();
            Txt_StringResult.Text = sh.ToSBC(Txt_stringBJ.Text);
        }

        /// <summary>
        /// 生成XML文件
        /// add by zhangyu
        /// 2013.01.17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_CreateXml_Click(object sender, EventArgs e)
        {
            XMLHelper xh = new XMLHelper();
            string XmlFilePath = Guid.NewGuid().ToString() + ".xml";
            XmlFilePath = Server.MapPath(XmlFilePath);

            string sContext = string.Empty;

            if (xh.CreateXmlDocument(XmlFilePath, "武大吉奥", "1.0", "UTF-8", "no"))
            {
                xh.CreateXmlNodeByXPath(XmlFilePath, "//武大吉奥", "工程中心", string.Empty, string.Empty, string.Empty);

                xh.CreateOrUpdateXmlNodeByXPath(XmlFilePath, "//武大吉奥//工程中心", "工程二部", "武大吉奥工程中心工程二部");

                sContext = File.ReadAllText(XmlFilePath, Encoding.GetEncoding("UTF-8"));

                if (File.Exists(XmlFilePath))
                    File.Delete(XmlFilePath);
            }
            else
            {
                sContext = "XML文件创建失败！";
            }
            Lb_XmlContext.Text = sContext;
        }
        
        /// <summary>
        /// 加密示例
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_ToDESE1_Click(object sender, EventArgs e)
        {
            DESEncrypt dese = new DESEncrypt();
            Txt_ToResult.Text = dese.Encrypt(Txt_Source.Text.Trim());        
        }
        /// <summary>
        /// 解密示例
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_ToSource_Click(object sender, EventArgs e)
        {
            DESEncrypt dese = new DESEncrypt();
            Lb_Source.Text = dese.Decrypt(Txt_ToResult.Text.Trim());
        }
    }
}